package com.solarStruggle.controls

import javax.swing.{JDialog}
import scala.swing.{Button, ButtonGroup, Frame, Label, RadioButton, TextField}
import scala.swing.event.{ButtonClicked}
import domain.{Game, GameCreator}

class NewGameScreen(state: GuiState)
extends JDialog(state.rootFrame.peer, "New Game", true) {
  
  val nameLabel = new Label("Name: ")
  val nameInput = new TextField
  
  val galaxySizeLabel = new Label("Galaxy Size (number of star systems): ")
  val smallGalaxyButton = new RadioButton("Small")
  val mediumGalaxyButton = new RadioButton("Medium")
  val largeGalaxyButton = new RadioButton("Large")
  val galaxySizeButtons = new ButtonGroup(smallGalaxyButton, mediumGalaxyButton, largeGalaxyButton)
  galaxySizeButtons.select(smallGalaxyButton)
  
  val galaxyDensityLabel = new Label("Galaxy Density (number of connections between star systems): ")
  val sparseGalaxyButton = new RadioButton("Sparse")
  val moderateGalaxyButton = new RadioButton("Moderate")
  val denseGalaxyButton = new RadioButton("Dense")
  val galaxyDensityButtons = new ButtonGroup(sparseGalaxyButton, moderateGalaxyButton, denseGalaxyButton)
  galaxyDensityButtons.select(moderateGalaxyButton);
  
  val solarSystemSizeLabel = new Label("Solar System Size: ")
  val smallSolarSystemButton = new RadioButton("Small")
  val mediumSolarSystemButton = new RadioButton("Medium")
  val largeSolarSystemButton = new RadioButton("Large")
  val solarSystemSizeButtons = new ButtonGroup(smallSolarSystemButton, mediumSolarSystemButton, largeSolarSystemButton)
  solarSystemSizeButtons.select(smallSolarSystemButton)
  
  val okButton = new Button("Ok")
  
  okButton.reactions += { case ButtonClicked(_) => 
    val numSolarSystems = galaxySizeButtons.selected match {
      case Some(x) if x == smallGalaxyButton => 10
      case Some(x) if x == largeGalaxyButton => 30
      case _ => 20
    }

    val numConnections = (numSolarSystems * (galaxyDensityButtons.selected match {
      case Some(x) if x == sparseGalaxyButton => 1.2
      case Some(x) if x == denseGalaxyButton => 3.0
      case _ => 2.0
    })).toInt
    
    val solarSystemSize = solarSystemSizeButtons.selected match {
      case Some(x) if x == smallSolarSystemButton => 10
      case Some(x) if x == largeSolarSystemButton => 30
      case _ => 20
    }
    
    state.currentGame = Some(GameCreator(nameInput.text, numSolarSystems, numConnections, solarSystemSize)) 
    GameCreator.save(state.currentGame.get)
    setVisible(false)
  }
  
  val cancelButton = new Button("Cancel")
  cancelButton.reactions += { case ButtonClicked(_) =>
    setVisible(false)
  }
  
  add(new MigPanel("", "[30%][][][]", "[][][100%]") {
    
    add(nameLabel)
    add(nameInput, "span 3, grow x, wrap")
    
    add(galaxySizeLabel)
    add(smallGalaxyButton)
    add(mediumGalaxyButton)
    add(largeGalaxyButton, "wrap")
    
    add(galaxyDensityLabel)
    add(sparseGalaxyButton)
    add(moderateGalaxyButton)
    add(denseGalaxyButton, "wrap")
    
    add(solarSystemSizeLabel)
    add(smallSolarSystemButton)
    add(mediumSolarSystemButton)
    add(largeSolarSystemButton, "wrap")
    
    add(okButton, "span 4, split 2, align right")
    add(cancelButton, "align right")
    
  }.peer)
  
}
